/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.lucava.boatskeeper.client.view.boat;

import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.SimplePager.TextLocation;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.MultiSelectionModel;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.lucava.boatskeeper.client.presenter.boat.BoatsPresenter;
import org.lucava.boatskeeper.shared.Boat;

/**
 *
 * @author glenn
 */
public class BoatsView extends Composite implements BoatsPresenter.Display {

    private final Button addButton;
    private final Button deleteButton;
    private final MultiSelectionModel<Boat> selectionModel;
    private final CellTable<Boat> table;
    private final Column<Boat, String> editColumn;
    private final SimplePager simplePager;
    private final ListDataProvider<Boat> dataProvider;

    public BoatsView() {

        DecoratorPanel panelDecorator = new DecoratorPanel();
        panelDecorator.setWidth("50em");

        // Create the menu

        HorizontalPanel hPanel = new HorizontalPanel();
        hPanel.setBorderWidth(0);
        hPanel.setSpacing(2);
        hPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_LEFT);
        addButton = new Button("Ajouter un Bateau");
        hPanel.add(addButton);
        deleteButton = new Button("Supprimer les bateaux sélectionnés");
        hPanel.add(deleteButton);





        table = new CellTable<Boat>();
        selectionModel = new MultiSelectionModel<Boat>();
        table.setSelectionModel(selectionModel);


        // Create name column.
        TextColumn<Boat> nameColumn = new TextColumn<Boat>() {
            @Override
            public String getValue(Boat boat) {
                return boat.getDisplayName();
            }
        };
        nameColumn.setSortable(true);
        // Create category column.
        TextColumn<Boat> categoryColumn = new TextColumn<Boat>() {
            @Override
            public String getValue(Boat boat) {
                return boat.getCategoryName();
            }
        };
        categoryColumn.setSortable(true);

        ButtonCell editButton = new ButtonCell();

        editColumn = new Column<Boat, String>(editButton) {
            @Override
            public String getValue(Boat object) {
                return "Edit";
            }
        };


        // Add the columns.

        table.addColumn(nameColumn, "Nom");
        table.setColumnWidth(nameColumn, 200, Unit.PX);

        table.addColumn(categoryColumn, "Catégorie");
        table.setColumnWidth(categoryColumn, 150, Unit.PX);

        table.addColumn(editColumn, "modifier");

        VerticalPanel vPanel = new VerticalPanel();
        vPanel.add(hPanel);

        dataProvider = new ListDataProvider<Boat>();
        SimplePager.Resources pagerResources = GWT.create(SimplePager.Resources.class);
        simplePager = new SimplePager(TextLocation.CENTER, pagerResources, true, 0, true);

        simplePager.setDisplay(table);
        simplePager.setPageSize(10);

        dataProvider.addDataDisplay(table);

        vPanel.add(table);
        vPanel.add(simplePager);

        panelDecorator.add(vPanel);

        initWidget(panelDecorator);
    }

    @Override
    public HasClickHandlers getAddButton() {
        return addButton;
    }

    @Override
    public HasClickHandlers getDeleteButton() {
        return deleteButton;
    }

    @Override
    public Column getEditColumn() {
        return editColumn;
    }

    @Override
    public List<Integer> getSelectedRows() {
        List<Integer> selectedRows = new ArrayList<Integer>();

        Iterator<Boat> itBoat = selectionModel.getSelectedSet().iterator();

        while (itBoat.hasNext()) {
            Integer id = itBoat.next().getId();
            selectedRows.add(id);
        }

        return selectedRows;
    }

    @Override
    public Widget asWidget() {
        return this;
    }

    @Override
    public void setData(List<Boat> data) {
        //table.setRowCount(data.size());
        //table.setRowData(data);
        dataProvider.setList(data);
        dataProvider.refresh();
    }

    @Override
    public void startProcessing() {
    }

    @Override
    public void stopProcessing() {
    }
}
